//
//  LKSportSpeaker.m
//  SportDemo
//
//  Created by Lukj on 2017/5/1.
//  Copyright © 2017年 lukj. All rights reserved.
//

#import "LKSportSpeaker.h"
#import <AVFoundation/AVFoundation.h>
@implementation LKSportSpeaker {
    //  运动类型字符串
    NSString *_typeStr;
    //  记录播报次数
    NSInteger _reportCount;
    //  苹果自带播报器
    AVSpeechSynthesizer *_synthesizer;
    AVPlayer *_avPlayer;
    //  音频索引缓存
    NSDictionary *_voiceDic;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        //  创建播报器
        _synthesizer = [[AVSpeechSynthesizer alloc] init];
        //  选用AVPlayer播放器, 可以进行曲目切换
        _avPlayer = [[AVPlayer alloc] init];
        //  解析音频索引列表
        NSString *path = [[NSBundle mainBundle] pathForResource:@"voice.bundle/voice.json" ofType:nil];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        _voiceDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    }
    return self;
}

- (void)startSportWithSportType:(SportTrackType)sportType {
    NSString *typeStr;
    switch (sportType) {
        case SportTrackTypeRuning:
            typeStr = @"跑步";
            break;
        case SportTrackTypewalking:
            typeStr = @"走路";
            break;
        case SportTrackTypebicycling:
            typeStr = @"骑行";
            break;
        default:
            break;
    }
    _typeStr = typeStr;
    [self reportContent:[NSString stringWithFormat:@"开始%@", typeStr]];
    
}


- (void)speakerSportState:(SportState)sportState {
    
    NSString *stateType;
    switch (sportState) {
        case SportStateContinue:
            stateType = @"运动已恢复";
            break;
        case SportStatePause:
            stateType = @"运动已暂停";
            break;
        case SportStateEnd:
            stateType = @"运动已结束";
            break;
        default:
            break;
    }
    
    [self reportContent:stateType];
    
}

- (void)speakerSportWithDistance:(double)distance andTime:(double)time andAvgSpeed:(double)avgSpeed {
    
//    CGFloat unitDistance = 1.f;
//    
//    if (distance >= (_reportCount + 1) * unitDistance) {
//        
//        NSString *content = [NSString stringWithFormat:@"你已经%@%.2f公里用时%.2f平均速度%.2f公里每小时太棒了", _typeStr, distance, time, avgSpeed];
//        [self reportContent:content];
//        
//        _reportCount++;
//    }
    
}


//  播报内容
- (void)reportContent:(NSString *)content {
    //  获取内容对应的文件名
    NSString *voiceName = _voiceDic[content];
    //  拼接路径
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"voice.bundle/%@",voiceName] ofType:nil];
    //  创建曲目对象
    AVPlayerItem *currentItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]];
    
    //  播放下一曲
    [_avPlayer replaceCurrentItemWithPlayerItem:currentItem];
    //  开始播放
    [_avPlayer play];
}

- (void)textReportContent:(NSString *)content {

    //  播报新内容时, 停止当前正在播报的内容
    [_synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    //  创建声音对象
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];
    //  播报内容
    [_synthesizer speakUtterance:utterance];
    
    NSLog(@"%@",content);

}

@end
